package business_opportunity

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/business_opportunity/customer"
	"github.com/gin-gonic/gin"
)

type CustomerGroupController struct {
	controller.BaseController
	customerGroupService customer.CustomerGroupServiceIface
}

func NewCustomerGroupController(
	customerGroupService customer.CustomerGroupServiceIface,
) *CustomerGroupController {
	return &CustomerGroupController{
		customerGroupService: customerGroupService,
	}
}

func (c *CustomerGroupController) RegisterRouter(g *gin.RouterGroup) {
	g.GET("customer-group/list", c.list)
}

func (c *CustomerGroupController) list(ctx *gin.Context) {
	request := &dto.GetCustomerGroupListRequest{}
	if err := ctx.ShouldBind(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}

	// 设置 租户 code
	request.TenantCode = authorize.GetTenantCode(ctx)
	// 1 的状态代表已经审核
	request.Status = 1
	response, err := c.customerGroupService.GetCustomerGroupList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}
